﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HYBYSoft.Company.Models;

namespace HYBYSoft.Company.WebSite.Areas.Admin.Controllers
{
    public class CompanyController : Controller
    {        
        /// <summary>
        /// 数据库的上下文
        /// </summary>
        CompanySiteEntities _db = new CompanySiteEntities();

        /// <summary>
        /// 公司基本信息的首页 Get提交
        /// </summary>
        /// <returns></returns>
        [Authentication]
        public ActionResult Index()
        {
            //获取公司的基本信息和联系人的集合，存储到自定义的ViewModel类中
            var companyInfo = (from company in _db.Companies
                               join link in _db.Linkmen on company.CompanyID equals link.CompanyID into Linkmanlist
                               select new CompanyAndLinkmanRelation
                               {
                                   CompanyID = company.CompanyID,
                                   Name = company.Name,
                                   Corporation = company.Corporation,
                                   Address = company.Address,
                                   TelFirst = company.TelFirst,
                                   TelSecond = company.TelSecond,
                                   TelThird = company.TelThird,
                                   Scale = company.Scale,
                                   Postcode = company.Postcode,
                                   Fax = company.Fax,
                                   Type = company.Type,
                                   Trade = company.Trade,
                                   Business = company.Business,
                                   Honor = company.Honor,
                                   E_mail = company.E_mail,
                                   City = company.City,
                                   Description = company.Description,
                                   Linkmen = Linkmanlist
                               }).First();

            //初始化页面所需加载的DropDownList项
            InitPage(companyInfo);

            return View(companyInfo);
        }


        /// <summary>
        /// 联系人信息
        /// </summary>
        /// <param name="lid">联系人id</param>
        /// <param name="type">类型为1代表是读取，为0代表为空</param>
        /// <returns></returns>
        public ActionResult ReadASCX(int lid,int type)
        {
            var linkman = new Linkman();
            if (type==1)
            {
                linkman = _db.Linkmen.FirstOrDefault(lm => lm.LinkmanID == lid);
                linkman.linkE_mail = linkman.E_mail;
                linkman.linkName = linkman.Name;
            }
            return PartialView("partialLinkMan", linkman);
        }

        /// <summary>
        /// post提交
        /// </summary>
        /// <param name="companyAndLinkman">强类型的ViewModel</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(CompanyAndLinkmanRelation companyAndLinkman)
        {


            //查询到与参数clr一致的Company实体
            var company = _db.Companies.First(c => c.CompanyID == companyAndLinkman.CompanyID);

            //构造实体类
            company.Address = companyAndLinkman.Address;
            company.Business = companyAndLinkman.Business;
            company.City = companyAndLinkman.City;
            company.CompanyID = companyAndLinkman.CompanyID;
            company.Corporation = companyAndLinkman.Corporation;
            company.Description = companyAndLinkman.Description;
            company.E_mail = companyAndLinkman.E_mail;
            company.Fax = companyAndLinkman.Fax;
            company.Honor = companyAndLinkman.Honor;
            company.Name = companyAndLinkman.Name;
            company.Postcode = companyAndLinkman.Postcode;
            company.Scale = companyAndLinkman.Scale;
            company.TelFirst = companyAndLinkman.TelFirst;
            company.TelSecond = companyAndLinkman.TelSecond;
            company.TelThird = companyAndLinkman.TelThird;
            company.Trade = companyAndLinkman.Trade;
            company.Type = companyAndLinkman.Type;

            //将修改更新到数据库
            _db.SaveChanges();

            //更改    获取页面Form中针对LinkMan实体命名的并带有Frist的TextBox的值，并将其更新入数据库中
            if (!string.IsNullOrEmpty(Request["linkName"]))
            {
                for (int i = 0; i < Request["linkName"].ToString().Split(',').Length; i++)
                {
                    if (!string.IsNullOrEmpty(Request["linkName"].Split(',')[i].ToString()))
                    {
                        int hiddenId = int.Parse(Request["linkManId"].Split(',')[i].ToString());
                        if (hiddenId != 0)
                        {
                            Linkman lm = _db.Linkmen.First(l => l.LinkmanID == hiddenId);
                            lm.CompanyID = companyAndLinkman.CompanyID;
                            lm.E_mail = Request["linkE_mail"].Split(',')[i].ToString();
                            lm.MSN = Request["MSN"].Split(',')[i].ToString();
                            lm.Name = Request["linkName"].Split(',')[i].ToString();
                            lm.QQ = Request["QQ"].Split(',')[i].ToString();
                            lm.Tel = Request["Tel"].Split(',')[i].ToString();
                            _db.SaveChanges();
                        }
                        else
                        {
                            Linkman lm = new Linkman();
                            lm.CompanyID = companyAndLinkman.CompanyID;
                            lm.E_mail = Request["linkE_mail"].ToString().Split(',')[i].ToString();
                            lm.MSN = Request["MSN"].ToString().Split(',')[i].ToString();
                            lm.Name = Request["linkName"].ToString().Split(',')[i].ToString();
                            lm.QQ = Request["QQ"].ToString().Split(',')[i].ToString();
                            lm.Tel = Request["Tel"].ToString().Split(',')[i].ToString();
                            _db.Linkmen.AddObject(lm);
                            _db.SaveChanges();
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return RedirectToAction("Index");
        }

        /// <summary>
        /// 删除联系人操作
        /// </summary>
        /// <param name="id">LinkMan的主键ID</param>
        /// <returns></returns>
        [Authentication]
        public ActionResult DeleteLinkMan(int id)
        {
            //删除联系人
            _db.Linkmen.DeleteObject(_db.Linkmen.FirstOrDefault(l => l.LinkmanID == id));
            _db.SaveChanges();

            return RedirectToAction("Index");
        }

        /// <summary>
        /// 初始化页面所需的加载项
        /// </summary>
        /// <param name="companyInfo">公司与联系人的信息</param>
        [NonAction]
        private void InitPage(CompanyAndLinkmanRelation companyInfo)
        {
            //dropdownlist 公司类型的数据源， 初始化所有选项都为不选中
            var itemsType = new List<SelectListItem>()
            {
                (new SelectListItem() {Text = "国营", Value = "国营", Selected = false}), 
                (new SelectListItem() {Text = "外资", Value = "外资", Selected = false}),
                (new SelectListItem() {Text = "合资", Value = "合资", Selected = false}),
                (new SelectListItem() {Text = "民营", Value = "民营", Selected = false})
            };

            //判断选项中是否有与数据库中的Type相同的值，若有则更新为选中项。
            itemsType.SingleOrDefault(item => item.Value == companyInfo.Type).Selected = true;

            //RadioButton 公司行业的数据源， 初始化所有选项都为不选中
            var itemsTrade = new List<SelectListItem>()
            {
                (new SelectListItem() {Text = "计算机", Value = "国营", Selected = false}), 
                (new SelectListItem() {Text = "金融", Value = "外资", Selected = false}),
                (new SelectListItem() {Text = "贸易", Value = "合资", Selected = false}),
                (new SelectListItem() {Text = "医疗", Value = "国营", Selected = false}), 
                (new SelectListItem() {Text = "广告", Value = "外资", Selected = false}),
                (new SelectListItem() {Text = "建筑", Value = "合资", Selected = false}),
                (new SelectListItem() {Text = "教育", Value = "国营", Selected = false}), 
                (new SelectListItem() {Text = "服务业", Value = "外资", Selected = false}),
                (new SelectListItem() {Text = "物流", Value = "国营", Selected = false}), 
                (new SelectListItem() {Text = "能源", Value = "外资", Selected = false}),
                (new SelectListItem() {Text = "其他", Value = "合资", Selected = false})
            };

            //dropdownlist 公司所在地的数据源， 初始化所有选项都为不选中
            var itemsCity = new List<SelectListItem>()
            {
                (new SelectListItem() {Text = "北京市", Value = "北京市", Selected = false}), 
                (new SelectListItem() {Text = "上海市", Value = "上海市", Selected = false}),
                (new SelectListItem() {Text = "天津市", Value = "天津市", Selected = false}),
                (new SelectListItem() {Text = "重庆市", Value = "重庆市", Selected = false}), 
                (new SelectListItem() {Text = "河北省", Value = "河北省", Selected = false}),
                (new SelectListItem() {Text = "山西省", Value = "山西省", Selected = false}),
                (new SelectListItem() {Text = "内蒙古自治区", Value = "内蒙古自治区", Selected = false}), 
                (new SelectListItem() {Text = "辽宁省", Value = "辽宁省", Selected = false}),
                (new SelectListItem() {Text = "吉林省", Value = "吉林省", Selected = false}),
                (new SelectListItem() {Text = "黑龙江省", Value = "黑龙江省", Selected = false}), 
                (new SelectListItem() {Text = "江西省", Value = "江西省", Selected = false}),
                (new SelectListItem() {Text = "浙江省", Value = "浙江省", Selected = false}),
                (new SelectListItem() {Text = "江苏省", Value = "江苏省", Selected = false}), 
                (new SelectListItem() {Text = "安徽省", Value = "安徽省", Selected = false}),
                (new SelectListItem() {Text = "福建省", Value = "福建省", Selected = false}), 
                (new SelectListItem() {Text = "山东省", Value = "山东省", Selected = false}),
                (new SelectListItem() {Text = "河南省", Value = "河南省", Selected = false}),
                (new SelectListItem() {Text = "湖北省", Value = "湖北省", Selected = false}), 
                (new SelectListItem() {Text = "湖南省", Value = "湖南省", Selected = false}),
                (new SelectListItem() {Text = "广东省", Value = "广东省", Selected = false}),
                (new SelectListItem() {Text = "广西自治区", Value = "广西自治区", Selected = false}),
                (new SelectListItem() {Text = "海南省", Value = "海南省", Selected = false}), 
                (new SelectListItem() {Text = "四川省", Value = "四川省", Selected = false}),
                (new SelectListItem() {Text = "贵州省", Value = "贵州省", Selected = false}),
                (new SelectListItem() {Text = "云南省", Value = "云南省", Selected = false}), 
                (new SelectListItem() {Text = "西藏自治区", Value = "西藏自治区", Selected = false}),
                (new SelectListItem() {Text = "陕西省", Value = "陕西省", Selected = false}),
                (new SelectListItem() {Text = "甘肃省", Value = "甘肃省", Selected = false}), 
                (new SelectListItem() {Text = "宁夏自治区", Value = "宁夏自治区", Selected = false}),
                (new SelectListItem() {Text = "新疆自治区", Value = "新疆自治区", Selected = false}),
                (new SelectListItem() {Text = "香港", Value = "香港", Selected = false}), 
                (new SelectListItem() {Text = "澳门特别行政区", Value = "澳门特别行政区", Selected = false}),
                (new SelectListItem() {Text = "中国台湾", Value = "中国台湾", Selected = false})
            };
            //判断选项中是否有与数据库中的City相同的值，若有则更新为选中项。
            itemsCity.SingleOrDefault(item => item.Value == companyInfo.City).Selected = true;

            ViewData["ddlType"] = itemsType;
            ViewData["rboListTrade"] = itemsTrade;
            ViewData["ddlCity"] = itemsCity;
        }

       
    }
}
